package trans.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import trans.model.Stations;
import trans.service.StationDAO;
import trans.support.BasicSupport;

import com.opensymphony.xwork2.Preparable;

public class StationMgrAction extends BasicSupport implements Preparable{

	private static final long serialVersionUID = 1L;
	
	Stations station;
	
	File upload;
	
    String uploadFileName;
    
    List<Stations> stationList;
    
    Integer stationId;
    
    String  flag;

	@Override
	public void prepare() throws Exception {
		
	}
	
	public String index() throws Exception{
		stationList = StationDAO.getStations();
		return "stationMgr";
	}
	
	public String addStation() throws Exception{
		StationDAO.addStation(station, upload, uploadFileName);
		addActionMessage("create an new Station success!");
		return index();
	}
	
	//显示上传的图片
	public String showFlagImage() throws Exception{
		return "showFlagImage";
	}
	
	public InputStream getInputStream() throws Exception {
		return new FileInputStream(StationDAO.getFlagFile(flag));
	}
	
	public String getFileName() throws Exception {
		return flag;
	}
	
	public String getFileType() throws Exception {
		if(flag.endsWith("gif")){return "image/gif";}
		if(flag.endsWith("jpg")){return "image/jpg";}
		if(flag.endsWith("jpeg")){return "image/jpeg";}
		if(flag.endsWith("png")){return "image/png";}
		return "application/octet-stream";
	}
	//显示上传的图片
	
	public String deleteStation() throws Exception{
		StationDAO.deleteStation(stationId);
		addActionMessage("delete an Station success!");
		return index();
	}

	public Stations getStation() {
		return station;
	}

	public void setStation(Stations station) {
		this.station = station;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<Stations> getStationList() {
		return stationList;
	}

	public void setStationList(List<Stations> stationList) {
		this.stationList = stationList;
	}

	public Integer getStationId() {
		return stationId;
	}

	public void setStationId(Integer stationId) {
		this.stationId = stationId;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
}
